﻿///就绪函数
$(function () {
    show();
});

///工人弹框添加
layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;

    // 打开添加弹窗的按钮点击事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                title: "工人工时管理/工人列表/添加",
                content: `
                    <form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人编号</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerNumbering" autocomplete="off" id="workerNumbering" placeholder="请输入工人编号" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人姓名</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerName" autocomplete="off" id="workerName" placeholder="请输入工人姓名" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">联系方式</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerPhone" autocomplete="off" id="workerPhone" placeholder="请输入联系方式" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-form">
                        <input type="radio" name="workerSex" value="男" title="男" checked>
                        <input type="radio" name="workerSex" value="女" title="女" >
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-col-md6">
                        <select name="workerDepartment" id="workerDepartment" >
                            <option value="">请选择部门</option>
                            <option value="生产部">生产部</option>
                            <option value="销售部">销售部</option>
                            <option value="指挥部">指挥部</option>
                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">班组</label>
                    <div class="layui-col-md6">
                        <select name="workerTeam" id="workerTeam" >
                            <option value="">请选择班组</option>
                            <option value="班组一">班组一</option>
                            <option value="班组二">班组二</option>
                            <option value="班组三">班组三</option>
                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-form">
                        <input type="radio" name="workerState" value="正常" title="正常" checked >
                        <input type="radio" name="workerState" value="离职" title="离职">
                    </div>
                    </div>
                    
                    <div class="layui-form-item">
                    <div class="layui-input-block">
                    <button class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
                     <button class="layui-btn" lay-submit lay-filter="demo-login">添加</button>
                    </div>
                    <form>`,
                success: function () {

                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-login)', function (data) {
                        var field = data.field;
                        console.log(field)
                        $.ajax({
                            url: '/Worker/GetAddWorker',
                            type: 'post',
                            data: { data: JSON.stringify(field), token: localStorage.getItem("token") },
                            success: function (res) {
                                var s = JSON.parse(res)
                                console.log(res)
                                if (s.data > 0) {
                                    layer.alert("添加成功");
                                    setTimeout(function () {
                                        location.reload();
                                    }, 2000);
                                }
                                else {
                                    layer.alert("添加失败");
                                }
                            }
                        })
                        return false; // 阻止默认 form 跳转
                    });
                }
            });

        }
    })
});


//工人显示
function show() {
    layui.use('table', function () {
        var table = layui.table;
        //查询
        var workerNumbering = $("#workerNumbering").val();
        var workerName = $("#workerName").val();
        var workerState = $("#workerState").val();
        $.ajax({
            url: "/Worker/GetShowWorker",
            type: "get",
            data: {
                workerNumbering: workerNumbering,
                workerName: workerName,
                workerState: workerState,
                pageIndex: 1,
                pageSize: 100,
                token: localStorage.getItem("token"),
            },
            success: function (res) {
                var res = JSON.parse(res);
                console.log(res.data)
                if (res.code == 0) {
                    console.log(res.data)
                    table.render({
                        elem: '#carriertable',//对应HTML中表格的id
                        data: res.data.plist,//绑定获取的数据
                        toolbar: '#toolbarDemo',//导出
                        defaultToolbar: ['exports', 'print'],//导出
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'workerNumbering', title: '工人编号', width: 120 },
                            { field: 'workerName', title: '工人姓名', width: 120 },
                            { field: 'workerPhone', title: '联系方式', width: 120 },
                            { field: 'workerTeam', title: '班组', width: 120 },
                            { field: 'workerDepartment', title: '部门', width: 120 },
                            { field: 'workerState', title: '状态', width: 120 },


                            {
                                field: '', title: '操作', templet: function (d) {
                                    return '<a class="layui-btn layui-btn-edit layui-btn-xs" lay-on="test-page-edit" onclick="updateData(' + d.workerId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                                        '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.workerId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>' +
                                        '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="storagemodel(' + d.workerId + ')"><i class="layui-icon layui-icon-search"></i>查看</a>' 
                                }
                            }
                        ]],//<button id="printButton" class="layui-btn layui-btn-normal">打印</button>
                        page: true, // 是否显示分页
                        count: res.data.listcount,
                        limits: [3, 4, 5],
                        limit: 3,// 每页默认显示的数量
                        curr: res.data.pagecount,
                    })
                }
                //else {
                //    console.log(res.msg);
                //}
            },
            error: function () {
                console.log("获取数据失败");
                // 可以考虑显示一个错误消息给用户
            }
        });


    })
}


//删除工人
window.deleteData = function (id) {
    console.log(id);
    layer.confirm('确定要删除该条数据吗？', function (index) {
        $.ajax({
            type: "Post",
            url: "/Worker/GetDeleteWorker",  // 替换为实际的后台删除数据接口地址
            data: { data: JSON.stringify({ workerId: id }), token: localStorage.getItem("token") },
            success: function (res) {
                var res = JSON.parse(res)
                if (res.data >= 1) {
                    layer.alert("删除成功");
                    show();  // 重新加载数据，刷新表格显示
                } else {
                    layer.alert("删除失败");

                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }
        });
        layer.close(index);
    });
};

//反填
var obj = {
    workerId: "",
    workerNumbering: "",
    workerName: "",
    workerPhone: "",
    workerSex: "",
    workerTeam: "",
    workerDepartment: "",
    workerState:""
}
//反填修改
window.updateData = function (id) {
    // 先根据id获取要编辑的数据，发起Ajax请求
    var form = layui.form;
    $.ajax({
        type: "get",
        url: "/Worker/GetBackFillWorker",  // 替换为实际的后台根据id获取数据的接口地址
        data: { workerId: id, token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            console.log(res)
            $(res.data).each(function () {
                $('#workerNumberings').val(this.workerNumbering)
                $('#workerNames').val(this.workerName)
                $('#workerPhone').val(this.workerPhone)
                //单选框反填
                var workerSex = this.workerSex
                $("[name='workerSex']").each(function () {
                    if (workerSex.toString()==$(this).val()) {
                        $(this).prop("checked", true);
                    }
                })
                workerSex
                $('#workerTeam').val(this.workerTeam)
                $('#workerDepartment').val(this.workerDepartment)
                var workerState = this.workerState
                $("[name='workerState']").each(function () {
                    if (workerState.toString()==$(this).val()) {
                        $(this).prop("checked", true);
                    }
                })
                obj.workerId = this.workerId
                obj.workerNumberings = this.workerNumbering
                obj.workerNames = this.workerName
                obj.workerPhone = this.workerPhone
                obj.workerSex = this.workerSex
                obj.workerTeam = this.workerTeam
                obj.workerDepartment = this.workerDepartment
                obj.workerState = this.workerState
                //渲染下拉框
                form.render('select');
                //渲染单选框
                form.render('radio');
            })

        },
    });
    layer.open({
        type: 1,
        title: '工人工时管理/工人列表/编辑',
        content: `<form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人编号</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerNumberings" autocomplete="off" id="workerNumberings" placeholder="请输入工人编号" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人姓名</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerNames" autocomplete="off" id="workerNames" placeholder="请输入工人姓名" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">联系方式</label>
                    <div class="layui-input-inline">
                    <input type="text" name="workerPhone" autocomplete="off" id="workerPhone" placeholder="请输入联系方式" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-form">
                        <input type="radio" name="workerSex" value="男" title="男">
                        <input type="radio" name="workerSex" value="女" title="女" >
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-col-md6">
                        <select name="workerDepartment" id="workerDepartment" >
                            <option value="">请选择部门</option>
                            <option value="生产部">生产部</option>
                            <option value="销售部">销售部</option>
                            <option value="指挥部">指挥部</option>
                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">班组</label>
                    <div class="layui-col-md6">
                        <select name="workerTeam" id="workerTeam" >
                            <option value="">请选择班组</option>
                            <option value="班组一">班组一</option>
                            <option value="班组二">班组二</option>
                            <option value="班组三">班组三</option>
                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-form">
                        <input type="radio" name="workerState" value="正常" title="正常" >
                        <input type="radio" name="workerState" value="离职" title="离职">
                    </div>
                    </div>

                    <div class="layui-form-item">
                    <div class="layui-input-block">
                    <div class="layui-form-item" style="text-align:center">
                    <button class="layui-btn" lay-submit lay-filter="demo-login">修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">取消</button>
              </div>
                    </div>
                    <form>
                    `,
        area: ['700px', '400px'],
        //修改
        success: function () {
            var obj1 = {
                workerId: "",
                workerNumbering: "",
                workerName: "",
                workerPhone: "",
                workerSex: "",
                workerTeam: "",
                workerDepartment: "",
                workerState: ""
            };
            console.log(obj1)
            // 对弹层中的表单进行初始化渲染
            form.render();
            // 表单提交事件
            form.on('submit(demo-login)', function (data) {
                var field = data.field;
                obj1.workerId = obj.workerId
                obj1.workerNumbering = field.workerNumberings
                obj1.workerName = field.workerNames
                obj1.workerPhone = field.workerPhone
                obj1.workerSex = field.workerSex
                obj1.workerTeam = field.workerTeam
                obj1.workerDepartment = field.workerDepartment
                obj1.workerState = obj.workerState
                $.ajax({
                    url: '/Worker/GetUpdateWorker',
                    type: 'post',
                    data: { obj1, token: localStorage.getItem("token") },
                    success: function (res) {
                        var res = JSON.parse(res)
                        console.log(field)
                        console.log(res)
                        if (res.data >= 1) {
                            layer.alert("修改成功");
                            setTimeout(function () {
                                location.reload();
                            }, 2000);
                        }
                        else {
                            layer.alert("修改失败");
                        }
                    }
                })
                return false; // 阻止默认 form 跳转
            });
        }
    })
}
//工人查看
var obj = {
    workerId: "",
    workerNumbering: "",
    workerName: "",
    workerPhone: "",
    workerSex: "",
    workerTeam: "",
    workerDepartment: "",
    workerState: ""
}
//反填修改
window.storagemodel = function (id) {
    // 先根据id获取要编辑的数据，发起Ajax请求
    var form = layui.form;
    $.ajax({
        type: "get",
        url: "/Worker/GetBackFillWorker",  // 替换为实际的后台根据id获取数据的接口地址
        data: { workerId: id, token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            console.log(res)
            $(res.data).each(function () {
                $('#workerNumberings').val(this.workerNumbering)
                $('#workerNames').val(this.workerName)
                $('#workerPhone').val(this.workerPhone)
                //单选框反填
                var workerSex = this.workerSex
                $("[name='workerSex']").each(function () {
                    if (workerSex.toString() == $(this).val()) {
                        $(this).prop("checked", true);
                    }
                })
                workerSex
                $('#workerTeam').val(this.workerTeam)
                $('#workerDepartment').val(this.workerDepartment)
                var workerState = this.workerState
                $("[name='workerState']").each(function () {
                    if (workerState.toString() == $(this).val()) {
                        $(this).prop("checked", true);
                    }
                })
                obj.workerId = this.workerId
                obj.workerNumberings = this.workerNumbering
                obj.workerNames = this.workerName
                obj.workerPhone = this.workerPhone
                obj.workerSex = this.workerSex
                obj.workerTeam = this.workerTeam
                obj.workerDepartment = this.workerDepartment
                obj.workerState = this.workerState
                //渲染下拉框
                form.render('select');
                //渲染单选框
                form.render('radio');
            })

        },
    });
    layer.open({
        type: 1,
        title: '工人工时管理/工人列表/编辑',
        content: `<form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人编号</label>
                    <div class="layui-input-inline" >
                    <input type="text" name="workerNumberings" autocomplete="off" id="workerNumberings" placeholder="请输入工人编号" disabled  class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">工人姓名</label>
                    <div class="layui-input-inline" >
                    <input type="text" name="workerNames" autocomplete="off" id="workerNames" placeholder="请输入工人姓名" disabled class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">联系方式</label>
                    <div class="layui-input-inline"  >
                    <input type="text" name="workerPhone" autocomplete="off" id="workerPhone" placeholder="请输入联系方式" disabled class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-form"  >
                        <input type="radio" name="workerSex" value="男" title="男" disabled >
                        <input type="radio" name="workerSex" value="女" title="女" disabled >
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-col-md6">
                        <select name="workerDepartment" id="workerDepartment" disabled >
                            <option value="">请选择部门</option>
                            <option value="生产部">生产部</option>
                            <option value="销售部">销售部</option>
                            <option value="指挥部">指挥部</option>
                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">班组</label>
                    <div class="layui-col-md6">
                        <select name="workerTeam" id="workerTeam" disabled >
                            <option value="">请选择班组</option>
                            <option value="班组一">班组一</option>
                            <option value="班组二">班组二</option>
                            <option value="班组三">班组三</option>
                        </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-form"  >
                        <input type="radio" name="workerState" value="正常" title="正常" disabled >
                        <input type="radio" name="workerState" value="离职" title="离职" disabled>
                    </div>
                    </div>

                    
                    <form>
                    `,
        area: ['700px', '400px'],
    })
}